import mysql.connector

# 生成测试账号

# 数据库连接配置
db_config = {
    'host': '3.1.52.81',
    'port': '3307',
    'user': 'root',
    'password': 'qPWMkvvs24YD8hItEo9M',
    'database': 'relax_cash'
}

def get_last_phone(cursor):
    """ 获取数据库中最后一个测试账号的手机号 """
    cursor.execute("SELECT phone FROM access_control_list ORDER BY id DESC LIMIT 1")
    result = cursor.fetchone()
    if result and result[0].isdigit():
        return int(result[0])
    return 91012340000  # 如果没有数据，返回默认起始值

def insert_test_accounts(num_records=10, remark="自动插入的测试账号"):
    """ 插入指定数量的测试账号 """
    try:
        conn = mysql.connector.connect(**db_config)
        cursor = conn.cursor()

        last_phone = get_last_phone(cursor)  # 获取最后一个测试账号
        print(f"数据库中最后一个测试账号: {last_phone}")

        insert_query = """
        INSERT INTO access_control_list (ctrl_type, phone, is_pass_audit_mode, remark)
        VALUES (%s, %s, %s, %s)
        """

        values_list = []
        for i in range(num_records):
            phone = str(last_phone + i + 1)  # 递增手机号
            values_list.append(('white', phone, 0, remark))
            print(f"即将插入: phone = {phone}, remark = {remark}")

        cursor.executemany(insert_query, values_list)  # 批量插入
        conn.commit()
        print(f"成功插入 {num_records} 条记录！")

    except mysql.connector.Error as err:
        print(f"数据库错误: {err}")
        conn.rollback()  # 遇到错误回滚事务
    finally:
        if conn.is_connected():
            cursor.close()
            conn.close()
            print("数据库连接已关闭。")

# 调用函数，示例：插入5条测试账号，remark为"测试账号"
insert_test_accounts(1, "158")
